home *** CD-ROM | disk | FTP | other *** search
/ Young Minds / Young Minds Interactive CD-ROM.ISO / crystal / cvsave.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-01-12  |  3.8 KB  |  156 lines

  1. /*    cvsave.c
  2.  *        routine to save or restore the user's game
  3.  *        only one game per user name is allowed in this version
  4.  *        what is saved:
  5.  *            a time-stamp of the program version (not the game)
  6.  *            all non-stack variables from cvmain.c
  7.  *            all variable info from cvlocs.c, cvobj.c
  8.  *************************************************************************/
  9.  
  10. #include    <string.h>
  11. #include    <stdio.h>
  12. #include    <errno.h>
  13. #include    <fcntl.h>
  14. #include    <sys/stat.h>
  15. #include    "cvobj.h"
  16. #include    "cvlocs.h"
  17.  
  18. #define    DIRNAME    "/usr/games/lib/crystal/"
  19. #define    DIRLEN    (sizeof DIRNAME)
  20. extern char *cuserid();
  21. extern char emain, datastart;
  22. extern int saved;
  23. static char savename[DIRLEN+L_cuserid] = DIRNAME;
  24. static unsigned datalen ;
  25. static unsigned conlen = sizeof(struct cvloc *) + sizeof(struct conn *);
  26.  
  27. long ptime = -1;        /* program time-stamp */
  28.  
  29. void
  30. cvsave()
  31. {    register struct cvloc *curloc;
  32.     register struct cvobj *curobj;
  33.     auto char userid[L_cuserid];
  34.     auto struct stat buf;
  35.     auto int save;
  36.  
  37.     datalen = &emain-&datastart;
  38.  
  39.     if (cuserid(userid) == NULL) {
  40.         puts("\nCannot save because I don't know who you are!");
  41.         return;
  42.     }
  43.     
  44.     (void) strcpy(savename+DIRLEN-1, userid);
  45.     if (stat(savename,&buf) == -1) {
  46.         if (errno != ENOENT) {
  47.             puts("\nCannot save because of directory trouble\n");
  48.             return;
  49.         }
  50.     } else {
  51.         if (yes(93,0,0)) {
  52.             if (unlink(savename) == -1) {
  53.                 printf("\nCannot unlink %s\n",savename);
  54.                 return;
  55.             }
  56.         } else { return; }
  57.     }
  58.     if ((save = open(savename,O_WRONLY|O_EXCL|O_CREAT,0600)) == -1) {
  59.         printf("\nCannot create %s\n",savename);
  60.         return;
  61.     }
  62.     if (write(save,&ptime,sizeof(long)) == -1
  63.         || write(save,&datastart,datalen) == -1) {
  64.         perror("Saving cave:");
  65.         close(save);
  66.         unlink(savename);
  67.         return;
  68.     }
  69.     for (curobj = cvobj; curobj->desc != NULL; curobj++) {
  70.         if (write(save, &(curobj->prop), sizeof(int)) == -1
  71.             || write(save, &(curobj->conn1.where),conlen) == -1
  72.             || write(save, &(curobj->conn2.where),conlen) == -1) {
  73.             perror("Saving cave objects");
  74.             close(save);
  75.             unlink(savename);
  76.             return;
  77.         }
  78.     }
  79.     for (curloc = cvloc; curloc->travel != NULL; curloc++) {
  80.         if (write(save, &(curloc->abb), sizeof(int)) == -1
  81.             || write(save, &(curloc->atloc.where),conlen) == -1) {
  82.             perror("Saving cave places");
  83.             close(save);
  84.             unlink(savename);
  85.             return;
  86.         }
  87.     }
  88.     (void) close(save);
  89.     return;
  90. }
  91.  
  92. void
  93. cvrest()
  94. {    register struct cvloc *curloc;
  95.     register struct cvobj *curobj;
  96.     auto char userid[L_cuserid];
  97.     auto int save;
  98.     auto long ftime = -1;
  99.  
  100.     datalen = &emain-&datastart;
  101.  
  102.     if (cuserid(userid) == NULL) {
  103.         puts("\nCannot restore because I don't know who you are!");
  104.         return;
  105.     }
  106.     
  107.     (void) strcpy(savename+DIRLEN-1, userid);
  108.  
  109.     if ((save = open(savename,O_RDONLY)) == -1) {
  110.         printf("\nCannot find %s\n",savename);
  111.         return;
  112.     }
  113.     if (read(save,&ftime,sizeof(long)) != sizeof(long)
  114. #ifdef    XVERSION
  115.         || ftime != ptime
  116. #endif
  117.         || read(save,&datastart,datalen) != datalen) {
  118.         if (ftime == -1) {
  119.             perror("Restoring cave:");
  120.         } else {
  121.             puts("\nSave file not created by this version");
  122.         }
  123.         close(save);
  124.         unlink(savename);
  125.         return;
  126.     }
  127.     for (curobj = cvobj; curobj->desc != NULL; curobj++) {
  128.         if (read(save, &(curobj->prop), sizeof(int)) != sizeof(int)
  129.             || read(save, &(curobj->conn1.where),conlen) != conlen
  130.             || read(save, &(curobj->conn2.where),conlen) != conlen) {
  131.             perror("Restoring cave objects");
  132.             close(save);
  133.             unlink(savename);
  134.             return;
  135.         }
  136.     }
  137.     for (curloc = cvloc; curloc->travel != NULL; curloc++) {
  138.         if (read(save, &(curloc->abb), sizeof(int)) != sizeof(int)
  139.             || read(save, &(curloc->atloc.where),conlen) != conlen) {
  140.             perror("Restoring cave places");
  141.             close(save);
  142.             unlink(savename);
  143.             return;
  144.         }
  145.     }
  146.     if (read(save, &ftime, 1) != 0) {
  147.         puts("\nNot end of file");
  148.         close (save);
  149.         unlink(savename);
  150.         exit(1);
  151.     }
  152.     (void) close(save);
  153.     if (saved == 1) saved = -1;
  154.     return;
  155. }
  156.